package com.hksj.Intelligentmanagement.auth.controller;

import com.hksj.Intelligentmanagement.auth.dto.RoleDTO;
import com.hksj.Intelligentmanagement.auth.dto.UserRoleDTO;
import com.hksj.Intelligentmanagement.auth.service.RoleService;
import com.hksj.Intelligentmanagement.auth.vo.RoleVo;
import com.hksj.Intelligentmanagement.auth.vo.UserRoleMenuVo;
import com.hksj.Intelligentmanagement.common.Rest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/api/role")
public class RoleController {

	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Resource
	public RoleService roleService;

	@GetMapping("getRole/{id}")
	@ApiOperation(value = "获取角色详情")
	public Rest<RoleVo> getRole(@ApiParam(value = "角色表id", required = true) @PathVariable("id") String id){
		return roleService.getRole(id);
	}

	@PostMapping("/createRole")
	@ApiOperation(value = "添加角色", notes = "添加角色")
	public Rest createRole(@RequestBody @Validated(RoleDTO.Create.class) RoleDTO role){
		return roleService.createRole(role);
	}

	@PostMapping("/updateRole")
	@ApiOperation(value = "修改角色")
	public Rest updateRole(@RequestBody @Validated(RoleDTO.Update.class) RoleDTO role){
		return roleService.updateRole(role);
	}

	@GetMapping("/deleteRole/{id}")
	@ApiOperation(value = "删除角色", notes = "删除角色")
	public Rest deleteRole(@ApiParam(value = "角色表id", required = true) @PathVariable("id") String id){
		return roleService.deleteRole(id);
	}

	@ApiOperation(value = "查询所有角色")
	@PostMapping("/listRole")
	public Rest<List<RoleVo>> listRole(
		@ApiParam(value = "角色名",required = false) @RequestParam(required = false) String name,
		@ApiParam(value = "角色权限字符串",required = false) @RequestParam(required = false) String roleKey,
		@ApiParam(value = "角色状态（0正常 1停用）",required = false) @RequestParam(required = false) String status){
		return roleService.listRole(name,roleKey,status);
	}

	@ApiOperation(value = "查询用户拥有的角色")
	@PostMapping("/listUserRole")
	public Rest<UserRoleMenuVo> listUserRole(
		@ApiParam(value = "用户id",required = true) @RequestParam(required = true) String userId){
		return roleService.listUserRoleWithMenus(userId);
	}

	@PostMapping("/assignRole")
	@ApiOperation(value = "分配用户角色")
	public Rest assignRole(@RequestBody UserRoleDTO userRole){
		return roleService.assignRole(userRole);
	}
}